#include "cals.h"
#include <math.h>
#include <QDebug>
#include <QTextStream>

const double volatge_range = 10;
const double ad_resolution_ratio = pow(2,15);
const double k = volatge_range/ad_resolution_ratio;
const double b = 0x8000;
const double high_power = pow(2,8);
const double frame_size = 8;//frame size:8

//calculate volatge from frame
// v = (high_value*high_power+low_value-b)*k
QList<double> frame2voltage(const QList<VCI_CAN_OBJ>& frames){
    double v{0},hv{0},lv{0};
    QList<double> voltages;
    for(int i=1;i<frames.size();++i){
        for(int j=0;j<frame_size;j+=2){
            hv = frames.at(i).Data[j];
            lv = frames.at(i).Data[j+1];
            v = (hv*high_power+lv-b)*k;
            voltages.append(v);
        }
    }
    return voltages;
}


bool read_xaxis(const QString& xpath, QList<double>& outs){
    //transform x
    QFile f(xpath);

    QTextStream stream(&f);

    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;
    try {
        while (!stream.atEnd()) {
            QString line = f.readLine();
            outs.append(line.toDouble());
        }
    } catch (int) {
        return false;
    }

    return true;
}

